package com.aurora.oa.work.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.utils.DateUtil;
import com.aurora.oa.work.bean.FileBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @Class: ListAdapter
 * @Description: 数据适配器
 */
public class FileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private boolean isNeedPullRefresh;
    private boolean hasMoreData = true;
    private Context context;
    private List<FileBean> mDatas;
    private OnItemClickListener mOnItemClickListener;

    public FileListAdapter(Context context, List<FileBean> mDatas, boolean isNeedPullRefresh) {
        this.mDatas = mDatas;
        this.context = context;
        this.isNeedPullRefresh = isNeedPullRefresh;
    }

    @Override
    public int getItemCount() {
        if (isNeedPullRefresh) {
            return mDatas.size() == 0 ? 0 : mDatas.size() + 1;
        } else {
            return mDatas.size() == 0 ? 0 : mDatas.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
        if (holder instanceof ItemViewHolder) {
            final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            //  itemViewHolder.sdvNoticeImage.setImageURI(AuroraConstants.BASE_SERVER_URL + "/" + mDatas.get(i).getCarImage());
            itemViewHolder.tvFileName.setText(mDatas.get(i).name);
            itemViewHolder.tvFileCreatTime.setText(DateUtil.getDateTime(mDatas.get(i).create_time));

            if (mOnItemClickListener != null) {
                /**
                 * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
                 * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
                 * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
                 */
                if (!itemViewHolder.itemView.hasOnClickListeners()) {
                    Log.e("ListAdapter", "setOnClickListener");
                    itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = itemViewHolder.getLayoutPosition();
                            mOnItemClickListener.onItemClick(v, pos);
                        }
                    });
                    itemViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            int pos = itemViewHolder.getLayoutPosition();
                            mOnItemClickListener.onItemLongClick(v, pos);
                            return false;
                        }
                    });
                }
            }

        } else if (holder instanceof FootViewHolder) {
            final FootViewHolder footViewHolder = (FootViewHolder) holder;
            if (!hasMoreData) {
                footViewHolder.content.setText("已经到底了");
                footViewHolder.progressBar.setVisibility(View.GONE);
            } else {
                footViewHolder.content.setText("数据加载中。。。");
                footViewHolder.progressBar.setVisibility(View.VISIBLE);
            }
        }

    }


    public void setHasMoreData(boolean hasMoreData) {
        this.hasMoreData = hasMoreData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        /**
         * 使用RecyclerView，ViewHolder是可以复用的。这根使用ListView的VIewHolder复用是一样的
         * ViewHolder创建的个数好像是可见item的个数+3
         */
        Log.e("ListAdapter", "onCreateViewHolder");
        if (!isNeedPullRefresh) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_file_list, parent,
                    false);
            return new ItemViewHolder(view);
        }

        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_file_list, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    /**
     * 向指定位置添加元素
     *
     * @param position
     * @param value
     */
    public void add(int position, FileBean value) {
        if (position > mDatas.size()) {
            position = mDatas.size();
        }
        if (position < 0) {
            position = 0;
        }
        mDatas.add(position, value);
        /**
         * 使用notifyItemInserted/notifyItemRemoved会有动画效果
         * 而使用notifyDataSetChanged()则没有
         */
        notifyItemInserted(position);
    }

    /**
     * 移除指定位置元素
     *
     * @param position
     * @return
     */
    public FileBean remove(int position) {
        if (position > mDatas.size() - 1) {
            return null;
        }
        FileBean value = mDatas.remove(position);
        notifyItemRemoved(position);
        return value;
    }


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    /**
     * 处理item的点击事件和长按事件
     */
    public interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onItemLongClick(View view, int position);
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        private ProgressBar progressBar;
        private TextView content;

        public FootViewHolder(View view) {
            super(view);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
            content = (TextView) view.findViewById(R.id.content);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_file_name)
        TextView tvFileName;
        @Bind(R.id.tv_file_creat_time)
        TextView tvFileCreatTime;

        ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}